package controllers

import (
	"newAi/new_web/models"
	"fmt"
	"strings"
)

type FlowCdrController struct {
	BaseController
}

func (self *FlowCdrController) FlowCdr() {
	callid := self.GetString("callid")
	self.Data["callid"] = callid
	self.display()
}

func (self *FlowCdrController) RuntimeList() {
	runtime, err := models.GetRunTimeList()
	if err != nil {
		fmt.Println(err)
	}
	a := models.RunTimeModel{}
	a.Call_id = "74d76eb5-f0a0-4009-9d8d-453a2eac4645"
	a.Call_phone = "15021026827"
	runtime = append(runtime, a)

	self.Data["runtime"] = runtime

	self.display()
}

func (self *FlowCdrController) NotMatchList() {
	self.Data["tplid"], _ = self.GetInt64("tplid")
	self.display()
}

func (self *FlowCdrController) Table() {
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}
	self.pageSize = limit
	tplid, _ := self.GetInt64("tplid")

	//查询条件
	filters := make([]interface{}, 0)
	filters = append(filters, "tpl_id", tplid)
	result, count := models.GetAiNotMatchList(page, self.pageSize, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["number"] = 30*page - 30 + k
		row["node_name"], _ = models.GetNodeName(v.Node_id, v.Tpl_id)
		row["words"] = v.Words
		row["tpl_id"] = v.Tpl_id
		row["node_id"] = v.Node_id
		row["uuid"] = v.Uuid
		if len(v.Record_file) > 0 {
			row["recode_file"] = Substr2(v.Record_file, strings.LastIndex(v.Record_file, "/")+1, len(v.Record_file))
		} else {
			row["recode_file"] = " "
		}
		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}

func (self *FlowCdrController) Deletenotmatch() {
	tplid, _ := self.GetInt64("tplid")
	err := models.DeleteNotMatchAllByTpl(tplid)
	if err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)
}

func (self *FlowCdrController) AjaxDel() {
	id, _ := self.GetInt("id")
	flowcdr, _ := models.GatNotMatchById(id)

	if err := flowcdr.Delete(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)
}

func (self *FlowCdrController) AjaxDatchDel() {
	data := self.GetString("data")
	data = strings.Trim(data, "[")
	data = strings.Trim(data, "]")
	datas := strings.Split(data,",")
	var sqldata = ""
	for index, value := range datas {
		if index == 0 {
			sqldata += "id="+value
		}else {
			sqldata += " OR id="+value
		}
	}

	if err := models.DatchDelete(sqldata); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("成功", MSG_OK)
}
